import { ref } from 'vue';
import api from 'axios';
import { hmiCatalogUrlAPI, hmiCatalogType } from 'src/util/HmiBaseConst';
import { cataItem } from 'src/hook/common/useCatalogType';

export function useHmiCatalogTbl () {
  const propDataTypeList = ref<cataItem[]>([]);//数据类型列表
  const propFormTypeList = ref<cataItem[]>([]);//输入表单类型列表
  const varTypeList = ref<cataItem[]>([]);//变量类型列表
  const encryptionTypeList = ref<cataItem[]>([]);//加密算法列表
  const protocolTypeList = ref<cataItem[]>([]);//通信协议列表
  const unitTypeList = ref<cataItem[]>([]);//数据单位列表
  const formulaTypeList = ref<cataItem[]>([]);//公式列表
  const operationTypeList = ref<cataItem[]>([]);//数据操作权限列表
  const lineStyleTypeList = ref<cataItem[]>([]);//线型列表
  const componentTypeList = ref<cataItem[]>([]);//组件类型列表
  const hmiComponentList = ref<cataItem[]>([]);//组件标识号列表
  const deviceTypeList = ref<cataItem[]>([]);//设备型号列表
  const optionTypeList = ref<cataItem[]>([]);//组件属性下拉框类型列表
  const componentFieldTypeList = ref<cataItem[]>([]);//组件行业分类列表

  const hmiDictTypeList = ref<cataItem[]>([]); //字典分类列表

  //路径
  let url = ref('');

  //从后台读取目录
  const getHmiCatalog = (catalog: hmiCatalogType) => {
    switch (catalog) {
      case hmiCatalogType.PROP_DATA_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.PROP_DATA_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            propDataTypeList.value = res.data.data;
          }
        })
        break;
      case hmiCatalogType.PROP_FORM_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.PROP_FORM_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            propFormTypeList.value = res.data.data;
          }
        })
        break;
      case hmiCatalogType.VAR_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.VAR_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            varTypeList.value = res.data.data;
          }
        })
        break;
      case hmiCatalogType.ENCRYPTION_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.ENCRYPTION_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            encryptionTypeList.value = res.data.data;
          }
        })
        break;
      case hmiCatalogType.PROTOCOL_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.PROTOCOL_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            protocolTypeList.value = res.data.data;
          }
        })
        break;

      case hmiCatalogType.UINT_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.UINT_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            unitTypeList.value = res.data.data;
          }
        })
        break;
      case hmiCatalogType.FORMULA_TPE_CODE:
        url.value = hmiCatalogUrlAPI.FORMULA_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            formulaTypeList.value = res.data.data;
          }
        })
        break;
      case hmiCatalogType.OPERATION_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.OPERATION_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            operationTypeList.value = res.data.data;
          }
        })
        break;
      case hmiCatalogType.LINE_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.LINE_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            lineStyleTypeList.value = res.data.data;
          }
        })
        break;

      case hmiCatalogType.OPTION_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.OPTION_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            optionTypeList.value = res.data.data;
          }
        })
        break;
      case hmiCatalogType.COMPONENT_FILED_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.COMPONENT_FILED_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            componentFieldTypeList.value = res.data.data;
          }
        })
        break;


      case hmiCatalogType.COMPONENT_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.COMPONENT_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            componentTypeList.value = res.data.data;
          }
        })
        break;
      case hmiCatalogType.COMPONENT_CODE:
        url.value = hmiCatalogUrlAPI.COMPONENT_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            hmiComponentList.value = res.data.data;
          }
        })
        break;
      case hmiCatalogType.DEVICE_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.DEVICE_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            deviceTypeList.value = res.data.data;
          }
        })
        break;
      case hmiCatalogType.DICT_TYPE_CODE:
        url.value = hmiCatalogUrlAPI.DICT_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            hmiDictTypeList.value = res.data.data;
          }
        })
        break;
      default:
        break;
    }
  };


  //释放目录
  const removeHmiCatalog = (catalog: hmiCatalogType) => {
    switch (catalog) {
      case hmiCatalogType.PROP_DATA_TYPE_CODE:
        propDataTypeList.value.length = 0;
        break;
      case hmiCatalogType.PROP_FORM_TYPE_CODE:
        propFormTypeList.value.length = 0;
        break;
      case hmiCatalogType.VAR_TYPE_CODE:
        varTypeList.value.length = 0;
        break;
      case hmiCatalogType.ENCRYPTION_TYPE_CODE:
        encryptionTypeList.value.length = 0;
        break;
      case hmiCatalogType.PROTOCOL_TYPE_CODE:
        protocolTypeList.value.length = 0;
        break;
      case hmiCatalogType.UINT_TYPE_CODE:
        unitTypeList.value.length = 0;
        break;
      case hmiCatalogType.FORMULA_TPE_CODE:
        formulaTypeList.value.length = 0;
        break;
      case hmiCatalogType.OPERATION_TYPE_CODE:
        operationTypeList.value.length = 0;
        break;
      case hmiCatalogType.LINE_TYPE_CODE:
        lineStyleTypeList.value.length = 0;
        break;

      case hmiCatalogType.OPTION_TYPE_CODE:
        optionTypeList.value.length = 0;
        break;
      case hmiCatalogType.COMPONENT_FILED_TYPE_CODE:
        componentFieldTypeList.value.length = 0;
        break;

      case hmiCatalogType.COMPONENT_TYPE_CODE:
        componentTypeList.value.length = 0;
        break;
      case hmiCatalogType.COMPONENT_CODE:
        hmiComponentList.value.length = 0;
        break;
      case hmiCatalogType.DEVICE_TYPE_CODE:
        deviceTypeList.value.length = 0;
        break;
      case hmiCatalogType.DICT_TYPE_CODE:
        hmiDictTypeList.value.length = 0;
        break;
      default:
        break;
    }
  }
  return {
    propDataTypeList,
    propFormTypeList,
    varTypeList,
    encryptionTypeList,
    protocolTypeList,
    unitTypeList,
    formulaTypeList,
    operationTypeList,
    optionTypeList,
    lineStyleTypeList,

    componentTypeList,
    hmiComponentList,
    deviceTypeList,
    hmiDictTypeList,
    componentFieldTypeList,

    getHmiCatalog,
    removeHmiCatalog
  }
}
